package com.jiezheng.huinong.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.jiezheng.huinong.db.conf.IPayManageDAO;
import com.jiezheng.huinong.model.PayInfo;
import com.jiezheng.huinong.util.DbUtil;


public class PayManageDAOImpl implements IPayManageDAO{

	@Override
	public void addPay(PayInfo pay) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.payManage.addPay",
					pay);
		} catch (Exception e) {
			// 此处由于不处理重复检验，很容易出现key重复的错误，直接忽略
			// 无需进一步处理
			//e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public Integer getTotalPayNum(String cond) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("payType", Integer.valueOf(cond));
		Integer totalNum = 0;
		try {
			totalNum = session.selectOne(
					"com.jiezheng.huinong.payManage.getTotalPayNum",
					rowInfo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<PayInfo> getPayInfo(String cond, Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<PayInfo> indexs = null;

		// 多值参数据的传递，包括起始行，与行数，实现分页
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("payType", Integer.valueOf(cond));

		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.payManage.getPayInfoByPage",
					rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}
}
